package com.haibei.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.RelativeLayout;

import com.haibei.R;
import com.haibei.adapter.AnimationAdapter;
import com.haibei.view.wheel.ArrayWheelAdapter;
import com.haibei.view.wheel.OnWheelChangedListener;
import com.haibei.view.wheel.WheelView;

public class StageWheelView extends RelativeLayout {

	private WheelView mWheelView;
	private StageSelectListener mSelectListener;
	// 女：备孕期 怀孕中 准妈妈
	// 男：备孕期 陪孕中 准爸爸
//	女的：没有宝宝、怀孕中、宝宝已出生；
//	男的：没有宝宝、陪孕中、宝宝已出生；
	
	private final String item_man[] = { "没有宝宝", "陪孕中", "宝宝已出生" };
	private final String item_woman[] = { "没有宝宝", "怀孕中", "宝宝已出生" };
	private final String item_values[]={"p","w","i"};
	private int selectIndex = 0;
	private boolean isMan;

	public StageWheelView(Context context, boolean isMan) {
		super(context);
		this.isMan = isMan;
		init();
	}

	public StageWheelView(Context context, AttributeSet attrs) {
		super(context, attrs);
		init();
	}

	private void init() {
		RelativeLayout.LayoutParams lp = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
		View v = View.inflate(getContext(), R.layout.wheel_stage, null);
		mWheelView = (WheelView) v.findViewById(R.id.wheelViewState);
		addView(v, lp);
		if (isMan) {
			mWheelView.setViewAdapter(new ArrayWheelAdapter<String>(getContext(), item_man));
		} else {
			mWheelView.setViewAdapter(new ArrayWheelAdapter<String>(getContext(), item_woman));
		}

		mWheelView.addChangingListener(new OnWheelChangedListener() {
			@Override
			public void onChanged(WheelView wheel, int oldValue, int newValue) {
				// if (null != mSelectListener) {
				// mSelectListener.onSelect(item[newValue],
				// String.valueOf(newValue));
				// }
			}
		});
		v.findViewById(R.id.cancel).setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				slideInout(StageWheelView.this, false, getContext());
			}
		});
		v.findViewById(R.id.submit).setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				slideInout(StageWheelView.this, false, getContext());
				if (null != mSelectListener) {
					int newValue = mWheelView.getCurrentItem();
					if (isMan) {
						mSelectListener.onSelect(item_man[newValue], String.valueOf(newValue),item_values[newValue]);
					} else {
						mSelectListener.onSelect(item_woman[newValue], String.valueOf(newValue),item_values[newValue]);
					}

				}
			}
		});
	}

	public void show() {
		slideInout(StageWheelView.this, true, getContext());
	}

	private static void slideInout(final View view, boolean inOut, Context context) {
		if (inOut) {
			// 出来
			Animation slideIn = AnimationUtils.loadAnimation(context, R.anim.slide_bottom_in);
			slideIn.setAnimationListener(new AnimationAdapter() {
				@Override
				public void onAnimationStart(Animation animation) {
					view.setVisibility(View.VISIBLE);
				}
			});
			view.startAnimation(slideIn);
		} else {
			Animation slideOut = AnimationUtils.loadAnimation(context, R.anim.slide_bottom_out);
			slideOut.setAnimationListener(new AnimationAdapter() {
				@Override
				public void onAnimationEnd(Animation animation) {
					view.setVisibility(View.GONE);
				}
			});
			view.startAnimation(slideOut);
		}
	}

	public static interface StageSelectListener {
		public void onSelect(String value, String text,String netValue);
	}

	public StageSelectListener getSelectListener() {
		return mSelectListener;
	}

	public void setSelectListener(StageSelectListener mSelectListener) {
		this.mSelectListener = mSelectListener;
	}

}
